package cc.jinglupeng.kom.pool;

import cc.jinglupeng.kom.util.KomConfig;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by jinglupeng on 4/10/16.
 */
public class ZookeeperPool extends GenericObjectPool<ZooKeeper> {

    private static Logger logger = LoggerFactory.getLogger(ZookeeperPool.class);
    private static ZookeeperPool zookeeperPool = new ZookeeperPool(new ZookeeperFactory(KomConfig.getZkConnectStrings(), KomConfig.getZkSessionTimeout()));


    private ZookeeperPool(ZookeeperFactory zookeeperFactory) {
        super(zookeeperFactory);
    }

    public static ZooKeeper borrowZooKeeper() throws Exception {
        return zookeeperPool.borrowObject();
    }

    public static void returnZooKeeper(ZooKeeper zooKeeper) {
        try {
            zookeeperPool.returnObject(zooKeeper);
        } catch (Exception e) {
            logger.error("catch exception when return zookeeper", e);
        }
    }

}
